home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualHFSFile.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  4.1 KB  |  182 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualHFSFile.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __VIRTUALHFSFILE__
  15. #include "VirtualHFSFile.h"
  16. #endif
  17.  
  18. /***********************************|****************************************/
  19.  
  20. #pragma segment VirtualFile
  21.  
  22. /***********************************|****************************************/
  23.  
  24. extern ostream& DumpHex (ostream& s, const void *p, unsigned long size);
  25.  
  26. /***********************************|****************************************/
  27.  
  28. inline long min ( long a, long b )
  29. {
  30.     return ( a < b) ? a : b;
  31. }
  32.  
  33. /***********************************|****************************************/
  34.  
  35. ostream& TVirtualHFSFile::operator >> ( ostream& s ) const
  36. {
  37.     s << "TVirtualHFSFile @ " << ( void*) this << ", " << fSpec;
  38.     return s << TAbstractFile::operator >> ( s );
  39. }
  40.  
  41. /***********************************|****************************************/
  42.  
  43. TVirtualHFSFile::TVirtualHFSFile(const FSSpec& location):
  44.     TVirtualFile (),
  45.     fSpec ( location ),
  46.     fRefNum ( -1 )
  47. {
  48.     Open ();
  49. }
  50.  
  51. /***********************************|****************************************/
  52.  
  53. TVirtualHFSFile::TVirtualHFSFile( short vRefNum, long dirID, const StringPtr name ):
  54.     TVirtualFile (),
  55.     fRefNum ( -1 )
  56. {
  57.     fSpec.vRefNum = vRefNum;
  58.     fSpec.parID = dirID;
  59.     ::PLstrcpy ( fSpec.name, name );
  60.     Open ();
  61. }
  62.  
  63. /***********************************|****************************************/
  64.  
  65. TVirtualHFSFile::~TVirtualHFSFile()
  66. {
  67.     Close ();
  68. }
  69.  
  70. /***********************************|****************************************/
  71.  
  72. void TVirtualHFSFile::Open ()
  73. {
  74.     if ( fRefNum == -1 )
  75.     {
  76.         ::FSpCreate(&fSpec, kDefaultCreator,kDefaultType, smRoman);
  77.         ::FSpOpenDF(&fSpec, fsRdWrPerm, &fRefNum);
  78.         SetPosition (fsFromStart, 0);
  79.     }
  80. }
  81.  
  82. /***********************************|****************************************/
  83.  
  84. void TVirtualHFSFile::Close ()
  85. {
  86.     if ( fRefNum != -1 )
  87.     {
  88.         ::FSClose(fRefNum);
  89.         fRefNum = -1;
  90.     }
  91. }
  92.  
  93. /***********************************|****************************************/
  94.  
  95. OSErr TVirtualHFSFile::DeleteFile()
  96. {
  97.     return ::FSpDelete(&fSpec);
  98. }
  99.  
  100. /***********************************|****************************************/
  101.  
  102. OSErr TVirtualHFSFile::ReadData (void* buffer,long& count)
  103. {
  104.     UpdateUsage();
  105.  
  106.     #if debug
  107.     if (steveFlag.Flag(18)) {
  108.         long pos; GetFPos(fRefNum, &pos);
  109.         keith << "TVHFSFile::Read " << count << " bytes. pos=" << pos << endl;
  110.     }
  111.     #endif        
  112.  
  113.     OSErr err = ::FSRead(fRefNum,&count,buffer);
  114.     
  115.     #if debug
  116.     if (steveFlag.Flag(18)) {
  117.         keith << "TVHFSFile::Read " << count << " bytes. err=" << err << endl;
  118.         DumpHex(keith, buffer, min ( count, 48 ) );
  119.     }
  120.     #endif        
  121.  
  122.     return err;
  123. }
  124.  
  125. /***********************************|****************************************/
  126.  
  127. OSErr TVirtualHFSFile::WriteData (const void* buffer, long& count)
  128. {
  129.     UpdateUsage();
  130.  
  131.     #if debug
  132.     if (steveFlag.Flag(18)) {
  133.         long pos; GetFPos ( fRefNum, &pos);
  134.         keith << "TVHFSFile::WriteData(" << count << ") bytes position=" << pos << endl;
  135.         DumpHex(keith, buffer, min ( count, 48 ) );
  136.     }
  137.     #endif        
  138.     
  139.     OSErr err = ::FSWrite(fRefNum,&count,buffer);
  140.     
  141.     #if debug
  142.     if (steveFlag.Flag(18)) {
  143.         long pos; GetFPos ( fRefNum, &pos);
  144.         keith << "TVHFSFile::WriteData(" << count << ") bytes. err=" << err << " newPos=" << pos << endl;
  145.     }
  146.     #endif    
  147.  
  148.     return err;
  149. }
  150.  
  151. /***********************************|****************************************/
  152.  
  153. OSErr TVirtualHFSFile::SetEnd (long logEof)
  154. {
  155.     UpdateUsage();
  156.     return ::SetEOF (fRefNum, logEof);
  157. }
  158.  
  159. /***********************************|****************************************/
  160.  
  161. OSErr TVirtualHFSFile::GetEnd (long& logEof) const
  162. {
  163.     return ::GetEOF (fRefNum, &logEof);
  164. }
  165.  
  166. /***********************************|****************************************/
  167.  
  168. OSErr TVirtualHFSFile::SetPosition (short posMode, long posOff)
  169. {
  170.     UpdateUsage();
  171.     return ::SetFPos (fRefNum,posMode,posOff);
  172. }
  173.  
  174. /***********************************|****************************************/
  175.  
  176. OSErr TVirtualHFSFile::GetPosition (long& filePos) const
  177. {
  178.     return ::GetFPos (fRefNum, &filePos);
  179. }
  180.  
  181. /***********************************|****************************************/
  182.